#include"SeqList.h"

void SLInit(SL* sl)
{ 
	sl->arr = NULL;
	sl->size = 0;
	sl->capacity = 0;
}

void SLcheckcapcity(SL* sl)
{
	if (sl->capacity == sl->size)
	{
		int newcapacity = sl->capacity == 0 ? 4 : 2 * sl->capacity;
		SLdatatype* temp = (SLdatatype*)realloc(sl->arr, newcapacity * sizeof(SLdatatype));
		if (temp == NULL)
		{
			perror("realloc failed");
			exit(1);
		}
		sl->arr = temp;
		sl->capacity = newcapacity;
	}
}

void SLpushback(SL* sl,SLdatatype x)
{
	assert(sl);
	SLcheckcapcity(sl);
	sl->arr[sl->size++] = x;

}
void SLpushfront(SL* sl, SLdatatype x)
{
	assert(sl);
	SLcheckcapcity(sl);
	int i = 0;
	for (i = (sl->size - 1); i >= 0; i--)
	{
		sl->arr[i + 1] = sl->arr[i];
	}
	sl->arr[0] = x;
	sl->size++;
}

void SLpopback(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}
void SLpopfront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	int i = 0;
	for (i=0; i <= ps->size-2 ; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

void SLinsert(SL* ps, int pos, SLdatatype x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLcheckcapcity(ps);
	int i = 0;
	for (i = ps->size-1; i >= pos; i--)
	{
		ps->arr[i + 1] = ps->arr[i];
	}
	ps->arr[pos] = x;
	ps->size++;
}

void SLerase(SL* ps, int pos)
{
	assert(ps);
	assert(ps->size);
	assert(pos >= 0 && pos < ps->size);
	for (int i = pos ; i < ps->size-1; i++)
	{
		ps->arr[pos] = ps->arr[pos + 1];
	}
	ps->size--;
}

int SLfind(SL* ps, SLdatatype x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		//if ( ps->arr[i]== x )
		//{
		//	return i;
		//}
	}
	return -1;
}

void SLDestory(SL* sl)
{
	assert(sl);
	if (sl->arr)
	{
		free(sl->arr);
	}
	sl->arr = NULL;
	sl->size = 0;
	sl->capacity = 0;
}
void SLPrintf(SL sl)
{
	int i = 0;
	for (i = 0; i < sl.size; i++)
	{
		//printf("%d ", sl.arr[i]);
	}
	printf("\n");

}